/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.matching.tagvalue;


/**
   This class implements the matching tree edge, which could be connected to
   some other tag node or could be a pure destination edge.
   MatchingTreeEdge.java
   @author Shrideep Pallickara
   $Revision$
   $Date$
*/
 

public class MatchingTreeEdge implements MatchingDebugFlags {

  private String value;
  /** If the value is satisfied the destinations could be fetched from the
      edge */
  private boolean providesDestinations;
  private boolean leadsToNode;

  private MatchingTreeNode leadsIntoNode;


  private EdgeAttributes edgeAttributes;

  /** This constructor creates an edge that leads into a certain node.
      @param value The value that this edge takes.
      @param leadsIntoNode The node that this edge leads into.
      @param destination The destination associated with this edge */
  public MatchingTreeEdge(String value, 
			  MatchingTreeNode leadsIntoNode,
			  int destination) {
    edgeAttributes = new EdgeAttributes();
    edgeAttributes.incrementPredicateCountForDestination(destination);
    this.value = value;
    this.leadsIntoNode = leadsIntoNode;
    leadsToNode = true;
    providesDestinations = false;
   }
  
  
  /** This constructor creates an edge that is a pure destination edge, 
      providing  destinations during the matching process. 
      @param value The value that this edge takes.
      @param destination The destination associated with this edge */
  public MatchingTreeEdge(String value, int destination) {
    edgeAttributes = new EdgeAttributes();
    edgeAttributes.incrementPredicateCountForDestination(destination);
    this.value = value;
    this.leadsIntoNode = null;
    leadsToNode = false;
    providesDestinations = true;
  }

  /** Provides an indication if this node leads into another tag node.
      @return <i>true</i> If it does lead into another node.
  */
  public boolean leadsIntoNode() {
    return leadsToNode;
  }
  
  /** Indicates if this edge is a pure destination edge, providing final
      destinations for a matched event.
      @return <i>true</i> If this edge is a pure destination edge. 
  */
  public boolean providesDestinations() {
    return providesDestinations;
  }
  
  /** Returns the destinations that are associated with the edge.
      @return Destinations associated with the edge.
   */
  public int getDestinations() {
    return edgeAttributes.getDestinations();
  }

  /** Returns the predicate count associated with the destinations 
      @return The predicate count associated with individual destinations.
  */
  public int[] getPredicateCountForDestinations() {
    return edgeAttributes.getPredicateCountForDestinations();
  }
  
  /** This method adds destinations to an edge. This occurs during reuse of 
      either a pure destination edge or an edge connecting two tags.
      @param _destination The destination associated with the edge in question.
  */
  public void addDestinationsToEdge(int _destination) {
    edgeAttributes.incrementPredicateCountForDestination(_destination);
  }
  
  /** This method removes destinations from an edge.
      @param _destination The destination associated with the edge in question.
  */
  public void removeDestinationsFromEdge(int _destination) {
    edgeAttributes.decrementPredicateCountForDestination(_destination);
  }
  
  /** This method adds a node to a certain edge.
      @param leadsIntoNode The node that this edge leads into */
  public void addNodeToEdge(MatchingTreeNode leadsIntoNode) {
    this.leadsIntoNode = leadsIntoNode;
    leadsToNode = true;
  }
  
  /** Returns value associated with the specific edge. Useful in the equals 
      method for the edge */
  public String getValue() {
    return value;
  }
  
  /** Returns the Tag node that this node leads into */
  public MatchingTreeNode getLeadingIntoNode() {
    return leadsIntoNode;
  }
  
  /**
     Compares two objects for equality. Returns a boolean that indicates
     whether this object is equivalent to the specified object. This method
     is used when an object is stored in a hashtable.
     @param obj the Object to compare with
     @return true if these Objects are equal; false otherwise.
     @see java.util.Hashtable
  */
  public boolean equals(Object obj) {
    if (value.equals(((MatchingTreeEdge)obj).getValue()) ) {
      if (leadsIntoNode.getTag().equals( ((MatchingTreeNode)obj).getTag())) {
	return true;
      }
    }
    return false;
  }
  
  /**
     Generates a hash code for the receiver. This method is supported 
     primarily for hash tables, such as those provided in java.util.
     If two objects are equal (equals(Object) returns true) they must have the 
     same hash code
     @return an integer hash code for the receiver 
  */
  public int hashCode() {
    return value.hashCode();
  }
  
  public static void main(String[] args) {
    
    
  }

}
